package com.yiwenup.leetcode.site;

import com.yiwenup.leetcode.ListNode;

/**
 * 文件描述
 *
 * @Project struct-algorithm
 * @Package com.yiwenup.leetcode.site
 * @Author yiwenup
 * @Date 2024-12-14 20:31:56
 * @Description https://leetcode.cn/problems/design-linked-list/
 */
public class No0707 {

    public static void main(String[] args) {
        MyLinkedList list = new MyLinkedList();
        list.deleteAtIndex(0);
    }

    static class MyLinkedList {
        int size;
        ListNode dummyHead;

        public MyLinkedList() {
            this.size = 0;
            this.dummyHead = new ListNode(-1);
        }

        public int get(int index) {
            if(index < 0 || index >= size) {
                return -1;
            }

            ListNode cur = this.dummyHead.next;
            while(index > 0) {
                index--;
                cur = cur.next;
            }
            return cur.val;
        }

        public void addAtHead(int val) {
            this.addAtIndex(0, val);
        }

        public void addAtTail(int val) {
            this.addAtIndex(size, val);
        }

        public void addAtIndex(int index, int val) {
            if(index < 0 || index > size) {
                return;
            }

            ListNode newNode = new ListNode(val);
            ListNode cur = this.dummyHead;
            while(index > 0) {
                index--;
                cur = cur.next;
            }
            newNode.next = cur.next;
            cur.next = newNode;

            this.size++;
        }

        public void deleteAtIndex(int index) {
            if(index < 0 || index >= size) {
                return;
            }
            ListNode cur = this.dummyHead;
            while(index > 0) {
                index--;
                cur = cur.next;
            }
            cur.next = cur.next.next;

            this.size--;
        }
    }
}
